package elementui.tools;

import elementui.base.Button;
import elementui.base.Icon;
import elementui.form.*;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.web.WebView;
import javafx.scene.input.MouseEvent;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.event.InputMethodEvent;
import java.awt.event.InputMethodListener;

/**
 * elementui.tools zgw --2020/7/1 3:07 下午
 * author javazgw@gmail.com
 */
public class CreateHtml extends JFrame {
    JFXPanel jfxPanel = new JFXPanel();
    JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);

    JTextPane jTextPane = new JTextPane();
    public CreateHtml()
    {
        init();
    }

    public void init()
    {


        Platform.runLater(()->{
            WebView  webView = new WebView();
            jfxPanel.setScene(new Scene(webView));
//            webView.getEngine().load("http://www.baidu.com");
            webView.getEngine().loadContent(jTextPane.getText(), "text/html");
            webView.getEngine().setUserStyleSheetLocation(getClass().getResource("../test/index.css").toString());
            webView.getEngine().setJavaScriptEnabled(true);

            jTextPane.getDocument().addDocumentListener(new DocumentListener() {
                @Override
                public void insertUpdate(DocumentEvent e) {
                    System.out.println("insert update");
                    Platform.runLater(()-> {
                        webView.getEngine().loadContent(jTextPane.getText(), "text/html");
                    });
                }

                @Override
                public void removeUpdate(DocumentEvent e) {
                    System.out.println("removeUpdate");
                }

                @Override
                public void changedUpdate(DocumentEvent e) {
                    System.out.println("changedUpdate");
                }
            });
        });



        jTextPane.addInputMethodListener(new InputMethodListener() {
            @Override
            public void inputMethodTextChanged(InputMethodEvent event) {
                System.out.println("input...");
            }

            @Override
            public void caretPositionChanged(InputMethodEvent event) {
                System.out.println("caretPositionChanged...");
            }
        });

        JScrollPane jScrollPane = new JScrollPane();
        jScrollPane.setViewportView(jTextPane);
        splitPane.setLeftComponent(jScrollPane);
        splitPane.setRightComponent(jfxPanel);

        pack();
        splitPane.setOneTouchExpandable(true);
        splitPane.setResizeWeight(0.5);
        add(splitPane);
        setVisible(true);

        setSize(1024,768);
    }


    public void setHmtl(String str)
    {
        jTextPane.setText(str);
    }
    public static String getHtmlDocument(String body,String data,String method)
    {
        StringBuffer sb = new StringBuffer();

        //文件头部

        sb.append("<!DOCTYPE html>\n" +
                "<html>\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <!-- import CSS -->\n" +
//                "    <link rel=\"stylesheet\" href=\"https://unpkg.com/element-ui/lib/theme-chalk/index.css\">\n" +
                "    <link rel=\"stylesheet\" href=\"./index.css\">\n" +
                "</head>\n" +
                "<body><div id=\"app\">");
        //生成的html  <div id="app"> </div>    的中间部分
        sb.append(body);
        //script外部引用
        sb.append("</div>\n" +
                "</body><!-- import Vue before Element -->\n" +
//                "<script src=\"https://unpkg.com/vue/dist/vue.js\"></script>\n" +
//                "<script src=\"./vue.js\"></script>\n" +
                "<script src=\"file:///Users/zgw/code/java/elemtui/out/production/elemtui/elementui/test/vue.js\"></script>\n" +
                "<!-- import JavaScript -->\n" +
//                "<script src=\"https://unpkg.com/element-ui/lib/index.js\"></script>\n" +
                "<script src=\"file:///Users/zgw/code/java/elemtui/out/production/elemtui/elementui/test/index.js\"></script>\n" +
                "<script>\n" +
                "    new Vue({\n" +
                "        el: '#app', ");
        //生成的js data data() {}
        sb.append(data);




        //生成的js method  method:{}
        sb.append(method);


        //结尾
        sb.append(" })\n" +
                "\n" +
                "</script>\n" +
                "</html>");



        return new String(sb);
    }



    public static void main(String[] argv)
    {




        String[][] items = new String[][]{
                {"bcode","姓名","input"},
                {"sex1","性别","checkbox"},
                {"birddate","日期","datePicker"},
                {"sex","性别2","Radio"},
                {"age","年龄","InputNumber"},
                {"like","爱好","Select"},
                {"auto","喜欢吃的","autocomplate"},
                {"fileset","资料","Upload"}



        };
        Form form = new Form();
        for(int i=0;i<items.length;i++)
        {
            FormItem formItem;
            switch (items[i][2].toLowerCase())
            {
                case "input":
                    formItem  = new Input();
                    formItem.setVmodel("form."+items[i][0]);
                    formItem.setTitle(items[i][1]);
                    formItem.setData(items[i][0]+":''");
                    break;
                case "checkbox":
                    formItem = new Checkbox();
                    formItem.setVmodel("form."+items[i][0]);
                    formItem.setTitle(items[i][1]);
                    formItem.setData(items[i][0]+":''");
                    break;
                case "datepicker":
                    formItem = new DatePicker();
                    formItem.setVmodel("form."+items[i][0]);
                    formItem.setTitle(items[i][1]);
                    formItem.setData(items[i][0]+":''");
                    break;
                case "radio":
                    formItem = new Radio();
                    formItem.setVmodel("form."+items[i][0]);
                    formItem.setTitle(items[i][1]);
                    formItem.setData(items[i][0]+":''");
                    break;
                case "inputnumber":
                    formItem = new InputNumber();
                    formItem.setVmodel("form."+items[i][0]);
                    formItem.setTitle(items[i][1]);
                    formItem.setData(items[i][0]+":''");
                    break;
                case "select":
                    formItem = new Select();
                    formItem.setVmodel("form."+items[i][0]);
                    formItem.setTitle(items[i][1]);
                    formItem.setData("options: [{\n" +
                            "          value: '选项1',\n" +
                            "          label: '黄金糕'\n" +
                            "        }, {\n" +
                            "          value: '选项2',\n" +
                            "          label: '双皮奶'\n" +
                            "        }, {\n" +
                            "          value: '选项3',\n" +
                            "          label: '蚵仔煎'\n" +
                            "        }, {\n" +
                            "          value: '选项4',\n" +
                            "          label: '龙须面'\n" +
                            "        }, {\n" +
                            "          value: '选项5',\n" +
                            "          label: '北京烤鸭'\n" +
                            "        }],\n" +
                            items[i][0] + "    : ''");
                    break;
                case "autocomplate":
                    formItem = new Autocomplate();
                    formItem.setVmodel("form."+items[i][0]);
                    formItem.setTitle(items[i][1]);
                    formItem.setData("options: [{\n" +
                            "          value: '选项1',\n" +
                            "          label: '黄金糕'\n" +
                            "        }, {\n" +
                            "          value: '选项2',\n" +
                            "          label: '双皮奶'\n" +
                            "        }, {\n" +
                            "          value: '选项3',\n" +
                            "          label: '蚵仔煎'\n" +
                            "        }, {\n" +
                            "          value: '选项4',\n" +
                            "          label: '龙须面'\n" +
                            "        }, {\n" +
                            "          value: '选项5',\n" +
                            "          label: '北京烤鸭'\n" +
                            "        }],\n" +
                            items[i][0] + "    : ''");
                    break;
                case "upload":
                    formItem = new Upload();
                    formItem.setVmodel("form."+items[i][0]);
                    formItem.setTitle(items[i][1]);
                    formItem.setData(items[i][0]+" : [{name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}, {name: 'food2.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}]");
                    ((Upload)formItem).setAction("http://192.168.1.21:58080/audit/up");
                    break;
                default:
                    formItem  = new Input();
                    formItem.setVmodel("form."+items[i][0]);
            }

            //formItem.setVmodel(items[i][0]);

            form.addFormItem(formItem);
        }















        CreateHtml createHtml = new CreateHtml();

        String html  = createHtml.getHtmlDocument(form.getFormStr()+"\n",form.getFormDataScript()+",",form.getFormMethodScript()
            );

        createHtml.setHmtl(html);
    }
}
